package com.open.capacity.notice.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.open.capacity.common.dto.ResponseEntity;
import com.open.capacity.notice.context.SendMessageJobContext;
import com.open.capacity.notice.dmo.SendMessageTask;
import com.open.capacity.notice.event.SendMessagePublisher;
import com.open.capacity.notice.service.SendMessageTaskService;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 前端控制器
 * </p>
 * 
 * @since 2024-05-09
 */
@RestController
@SuppressWarnings("all")
@RequiredArgsConstructor
@RequestMapping("/sendMessageTask")
public class SendMessageTaskController {

	private final SendMessageTaskService sendMessageTaskService;
	private final SendMessagePublisher sendMessagePublisher;

	/**
	 * 分页列表查询
	 * 
	 * @param sendMessageTask
	 * @param page
	 * @return
	 */
	@ApiOperation(value = "子任务管理-分页列表查询", notes = "子任务管理-分页列表查询")
	@GetMapping(value = "/list")
	public ResponseEntity queryPageList(SendMessageTask sendMessageTask, Page page) {
		IPage<SendMessageTask> pageList = sendMessageTaskService.page(page, Wrappers.query(sendMessageTask));
		return ResponseEntity.succeed(pageList);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "子任务管理-通过id查询", notes = "子任务管理-通过id查询")
	@GetMapping(value = "/query-by-id")
	public ResponseEntity<SendMessageTask> queryById(@RequestParam(name = "id") String id) {
		return ResponseEntity.succeed(sendMessageTaskService.getById(id));
	}

	/**
	 * 编辑
	 *
	 * @param sendMessageTask
	 * @return
	 */
	@ApiOperation(value = "子任务管理-编辑", notes = "子任务管理-编辑")
	@PostMapping(value = "/edit")
	public ResponseEntity<Boolean> edit(@Valid @RequestBody SendMessageTask sendMessageTask) {
		return ResponseEntity.succeed(sendMessageTaskService.updateById(sendMessageTask), "编辑成功");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "子任务管理-通过id删除", notes = "子任务管理-通过id删除")
	@PostMapping(value = "/delete")
	public ResponseEntity<Boolean> delete(@RequestParam(name = "id") String id) {
		boolean result = sendMessageTaskService.removeById(id);
		return ResponseEntity.succeed(result, "删除成功");
	}

	/**
	 * 重新发送
	 * @param sendMessageTasks
	 * @return
	 */
	@ApiOperation(value = "子任务管理务-重新发送", notes = "子任务管理-重新发送")
	@PostMapping(value = "/resend")
	public ResponseEntity resend(@Valid @RequestBody List<SendMessageTask> sendMessageTasks) {
		sendMessagePublisher.publish(sendMessageTasks, new SendMessageJobContext());
		return ResponseEntity.succeed("重新发送");
	}

}
